import Cookie from 'js-cookie'
export default {
    state: {
        /*左侧栏是否水平展开*/
        isCollapse: false,
        currentMenu: null,
        menu: [],
        tabsList: [
            {
                path: '/',
                name: 'home',
                label: '首页',
                icon: 'home'
            }
        ]
    },
    mutations: {
        setMenu(state, val) {
            //vuex添加
            state.menu = val
            //cookie也添加
            Cookie.set('menu', JSON.stringify(val))
        },
        setUserInfo(state, val) {
            state.userInfo = val;
        },
        clearMenu(state) {
            //清除也一样 vuex和cookie都清除
            state.menu = []
            Cookie.remove('menu')
        },
        addMenu(state, router) {
            if (Cookie.get('menu')) {
                /*console.log(Cookie.get('menu'))*/
                //取出cookie数据 给vuex
                let menu = JSON.parse(Cookie.get('menu'))
                state.menu = menu
                //添加动态路由 主路由为Main.vue
                let currentMenu = [
                    {
                        path: '/',
                        component: () => import(`@/views/Main`),
                        children: []
                    }
                ]
                //如果是一级菜单 那么菜单名称肯定有路由 如果是二级菜单那么一级没有 二级有路由
                menu.forEach(item => {
                    /* 判断是否有子节点，如果有*/
                    if (item.children) {
                        currentMenu[0].children.push(...resolveChildren(item.children))
                    } else {
                        item.path = item.routerPath;
                        item.name = item.routerCode;
                        item.meta = {};
                        item.meta.btnPerms = item.btnPerms;
                        item.component = () => import(`@/views/${item.fileUrl}`)
                        currentMenu[0].children.push(item)
                    }
                })
                //添加动态路由
                router.addRoutes(currentMenu)
            }
        },

        //选择标签 选择面包屑
        selectMenu(state, val) {
            if (val.name !== 'home') {
                state.currentMenu = val
                let result = state.tabsList.findIndex(item => item.name === val.name)
                result === -1 ? state.tabsList.push(val) : ''
                Cookie.set('tagList', JSON.stringify(state.tabsList))
            } else {
                state.currentMenu = null
            }
        },
        //获取标签
        getMenu(state) {
            if (Cookie.get('tagList')) {
                let tagList = JSON.parse(Cookie.get('tagList'))
                state.tabsList = tagList
            }
        },
        //关闭标签
        closeTab(state, val) {
            let result = state.tabsList.findIndex(item => item.name === val.name)
            state.tabsList.splice(result, 1)
            Cookie.set('tagList', JSON.stringify(state.tabsList))
        },
        //左侧栏是否水平展开
        collapseMenu(state) {
            state.isCollapse = !state.isCollapse
        }
    },
    actions: {}
}
export function resolveChildren(children) {
    return children.map(item => {
        item.path = item.routerPath;
        item.name = item.routerCode;
        item.meta = {};
        item.meta.btnPerms = item.btnPerms;
        item.component = () => import(`@/views/${item.fileUrl}`)
        if (item.children) {
            resolveChildren(item.children);
        }
        return item
    })
}
